﻿using Station.Common.Rejudgment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Station.Inspect.Rejudgment
{
    public class InspectResult
    {
        /// <summary>
        /// 检测结果
        /// </summary>
        public List<ErrList> ErrLst { get; set; } = new List<ErrList>();


        private CellInfo[] cellInfos;
        /// <summary>
        /// cell信息
        /// </summary>

        public CellInfo[] CellInfos { get => cellInfos; }
        public InspectResult(int size)
        {
            if (size > 0)
            {
                cellInfos = new CellInfo[size];
                for (int i = 0; i < cellInfos.Length; i++)
                {
                    cellInfos[i] = new CellInfo();
                }
            }
        }

        public void Add(ErrList err)
        {
            ErrLst.Add(err);
            foreach (var camera in err.ErrCameras)
            {
                foreach (var comp in camera.ErrComponents)
                {
                    foreach (var cell in comp.ErrCells)
                    {
                        int id = cell.ID - 1;
                        if (id >= 0 && id < cellInfos.Length)
                        {
                            cellInfos[id].Add(err.FaceName, cell);
                        }
                    }
                }
            }
        }
        public Thread SaveThread;    //保存线程
        public void Save(string code = "")
        {
            if (SaveThread != null)
            {
                SaveThread.Abort();
            }
            SaveThread = new Thread(new ParameterizedThreadStart(SaveImg));
            SaveThread.IsBackground = true;
            SaveThread.Start(code);
        }
        public event EventHandler<string> OnSaveImgRecive;
        // private static int isComplete = 0;
        public void SaveImg(object data)
        {
            //SemaphoreSlim sem = new SemaphoreSlim(0, 30);
            //isComplete = 0;
            StaInspect.Instance.InitCanvas();
            if (StaInspect.Instance.isMakeBigImg)
                if (StaInspect.Instance.canvasB == null || StaInspect.Instance.canvasA == null)
                {
                    return;
                }
            //ThreadPool.SetMaxThreads(30, 30);
            //ErrList.gA = gA;
            //ErrList.gB = gB;
            string code = (string)data;
            var tasks = new Task[ErrLst.Count];
            if (string.IsNullOrEmpty(code))
            {
                int i = 0;
                foreach (var item in ErrLst)
                {

                    tasks[i] = Task.Run(() =>
                    {
                        savelist(item, code);
                    });
                    i++;
                }
                //--
                //for (int i = 0; i < ErrLst.Count; i++)
                //{
                //    //tasks[i] = Task.Run(() =>
                //    //{
                //    //    savelist(ErrLst[i], code);
                //    //});
                //    //ThreadPool.QueueUserWorkItem(new WaitCallback(savelist), new object[] { ErrLst[i], code, sem });
                //}
                // Directory.CreateDirectory($"{StaInspect.Instance.loadPath}\\Savemap\\{code}\\");
                //gA = ErrList.gA;
                //gB = ErrList.gB;
                Task.WaitAll(tasks);
                Console.WriteLine("等待线程池完成操作。。。"); //等待事件的完成，即线程调用ManualResetEvent.Set()方法
                StaInspect.Instance.SaveBigMap(code);
                return;
            }



            if (code.IndexOf(",") > -1)
            {
                var pos = code.Split(',');

                int i = 0;
                foreach (var item in ErrLst)
                {

                    tasks[i] = Task.Run(() =>
                    {
                        savelist(item, pos[0].ToString());
                    });
                    i++;
                }

                //for (int i = 0; i < ErrLst.Count; i++)
                //{
                //    //tasks[i] = Task.Run(() =>
                //    //{
                //    //    savelist(ErrLst[i], pos[0]);
                //    //});
                //    //ThreadPool.QueueUserWorkItem(new WaitCallback(savelist), new object[] { ErrLst[i], pos[0], sem });
                //}

                //gA = ErrList.gA;
                //gB = ErrList.gB;
                //_ManualEvents.WaitOne();
                //sem.Release(30);

                //while (true)
                //{
                //    if (isComplete >= ErrLst.Count)
                //    {
                //        break;
                //    }
                //    Thread.Sleep(10);
                //}
                Task.WaitAll(tasks);
                Console.WriteLine("等待线程池完成操作。。。"); //等待事件的完成，即线程调用ManualResetEvent.Set()方法

                StaInspect.Instance.SaveBigMap(pos[0]);
                OnSaveImgRecive?.Invoke(this, pos[1]);

            }


        }

        public static void savelist(ErrList item, string code)
        {
            //object[] parameters = state as object[];
            //SemaphoreSlim sem = (SemaphoreSlim)parameters[2];
            //ErrList item = (ErrList)parameters[0];
            //string code = (string)parameters[1];
            //sem.Wait();
            //Graphics gA = (Graphics)parameters[2];
            //Graphics gB = (Graphics)parameters[3];
            item.Save(code);
            //sem.Release();
            //isComplete++;
            //ManualResetEvent e = (ManualResetEvent)parameters[2];
            //e.Set();
        }
    }
}
